package 马踏棋盘;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class View {
	public void Main() {
		EventQueue.invokeLater(() -> {
			//JFrame frame = new FristFrame();
			FristFrame frame = new FristFrame();
			frame.setTitle("JAVA兴趣小组");//设置标题
			//定义用户关闭框架时的响应动作
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setLocationByPlatform(true);//系统自动确定窗口位置
			for(int i = 0; i < 10 ;i++) {
				frame.add(new PrintPiceComponent(i*30, i*30));
				frame.setVisible(true);
				frame.print(frame.getGraphics());

				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
				//p.setPice(i ,i);
				//frame.paint(frame.getGraphics());
			}
			
			//frame.remove(p);
			//frame.setVisible(true);
		});
	}
}

class FristFrame extends JFrame{//绘制框架
	
	private static final long serialVersionUID = 1L;
	//private static final int DEFAULT_WIDTH = 1280;
	//private static final int DEFAULT_HEIGHT = 720;
	public FristFrame() {
		/*获取屏幕宽高*/
		/*
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		//int screenWidth = screenSize.width;
		//int screenHeight = screenSize.height;
		setSize(screenWidth/2 ,screenWidth/2);
		*/
		Image img = new ImageIcon("icon.gif").getImage();
		setIconImage(img);
		add(new MyComponent());
		pack();
	}
}
class Test extends JPanel{
	
}
@SuppressWarnings("serial")
class MyComponent extends JComponent{
	static int w = 30;//棋盘边框宽度
	static int w2 = 50; //棋盘小格宽度
	public void paintComponent(Graphics g) {
		//该方法由系统自动调用  无需手动调用
		Graphics2D g2 = (Graphics2D) g;
		g2.setPaint(Color.black);
		Rectangle2D rect = new Rectangle2D.Double(w,w,Board.getN()*w2,Board.getN()*w2);
		g2.draw(rect);
		for(int i = 1; i < Board.getN(); i++) {
			Line2D.Double line1 = new Line2D.Double(w,i*w2+w , Board.getN()*w2+w ,i*w2+w);
			Line2D.Double line2 = new Line2D.Double(i*w2+w ,w ,i*w2+w ,Board.getN()*w2+w);
			g2.draw(line1);
			g2.draw(line2);
		}
		//g2.drawString("java兴趣小组无敌", 100, 200);
	}
	
	public Dimension getPreferredSize() {
		return new Dimension(Board.getN()*w2+2*w, Board.getN()*w2+2*w);
	}
}
@SuppressWarnings("serial")
class PrintPiceComponent extends JComponent{
	int w = 5; //棋子和格子的间隔
	int xl = MyComponent.w + w;
	int yl = MyComponent.w + w;
	int ww = MyComponent.w2-2*w; //棋子的宽高
	
	public PrintPiceComponent(int x ,int y) {
		setPice(x, y);
	}
	public void  setPice(int x ,int y) {
		xl = x;
		yl = y;
	}
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setPaint(Color.black);
		Rectangle2D rect = new Rectangle2D.Double(xl,yl,ww ,ww);
		Ellipse2D ellipse = new Ellipse2D.Double();
		ellipse.setFrame(rect);
		g2.fill(ellipse);
	}
}
